package com.qidian.mes.tools.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.pojo.ToolLedger;
import com.qidian.mes.tools.mapper.ToolsLedgerMapper;
import com.qidian.mes.tools.service.ToolsLegerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 任超
 * Time 2024/10/21 11:31
 */
@Service
public class ToolsLedgerServiceImpl implements ToolsLegerService {

    @Autowired
    private ToolsLedgerMapper toolsLedgerMapper;

    @Override
    public MESResult selectList(ToolLedger toolLedger, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<ToolLedger> toolLedgerList = toolsLedgerMapper.list(toolLedger);
        PageInfo<ToolLedger> pageInfo = new PageInfo<>(toolLedgerList);
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult addToolLedger(ToolLedger toolLedger) {
        if (toolsLedgerMapper.selectByCode(toolLedger.getToolLedgerCode())!=null)
            return MESResult.error();
        if (toolsLedgerMapper.addToolLedger(toolLedger)==1)
            return MESResult.success(toolLedger);
        return MESResult.error();
    }

    @Override
    public MESResult updateToolLedger(ToolLedger toolLedger) {
        ToolLedger temp = toolsLedgerMapper.selectByCode(toolLedger.getToolLedgerCode());
        if (temp != null && !temp.getToolLedgerCode().equals(toolLedger.getToolLedgerCode()))
            return MESResult.error();
        else
            toolsLedgerMapper.updateLedger(toolLedger);
        return MESResult.success();
    }

    @Override
    public MESResult selectById(Long toolLedgerId) {
        return MESResult.success(toolsLedgerMapper.selectById(toolLedgerId));
    }

    @Override
    public MESResult deleteToolLedger(Long toolLedgerId) {
        return MESResult.success(toolsLedgerMapper.deleteLedger(toolLedgerId));
    }

    @Override
    public MESResult selectByType(int status) {
        return MESResult.success(toolsLedgerMapper.selectType(status));
    }
}
